﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Wedo.Domain.Models;
using Wedo.Recognition.Domain;
using Wedo.Mvc.Utility;
using Wedo.Recognition.Repository;
using Wedo.Recognition.Service.CommonService;
using Wedo.Mvc.Pager;

namespace Wedo.Recognition.Web.Controllers
{ 
    public class ResourceController :EntityKeyController<Resource, Guid,SearchModel>
    {
        private ResourceService service;
        private IUnitWork _dbContext;

        public ResourceController(IUnitWork dbContext)
        {
            _dbContext = dbContext;
            service = new ResourceService(_dbContext);
        }
      
        protected override void Dispose(bool disposing)
        {
            _dbContext.Dispose();
            base.Dispose(disposing);
        }

        protected override void Insert(Resource model)
        {
            service.Add(model);
        }

        protected override void DeleteModel(Guid id)
        {
            service.Remove(id);
        }

        protected override void UpdateModel(Resource model)
        {
            service.Update(model);

        }

        protected override Resource FindById(Guid Id)
        {
            var modules = _dbContext.CacheSet<Module>().CachedAll();
            var model=service.GetById(Id);
            ViewData["Module_Id"] = new SelectList(modules,"Id","Name",model.Module_Id);
            return model;
        }
        public override ActionResult Create()
        {
            var modules = _dbContext.CacheSet<Module>().CachedAll();
            ViewData["Module_Id"] = new SelectList(modules, "Id", "Name");

            return base.Create();
        }

        protected override Wedo.Mvc.Pager.PagedList<Resource> GetSearchPagedList(SearchModel where)
        {
            where=where??new SearchModel();
            var query = service.All();
            if (where.Keyword.IsNotNullOrWhiteSpace())
            {
                query = query.Where(m => m.Action.Contains(where.Keyword) ||
                    m.Controller.Contains(where.Keyword)
                    || m.Name.Contains(where.Keyword));
            }
            return query.ToPagedList(where.Page,10);
        }

    }
}